﻿using ET_DbCommon.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET_DbCommon.DAL
{
    public static class Exam_TestQuestion_TaskDAL
    {
        /// <summary>
        /// 创建考核关联的场景任务
        /// </summary>
        /// <param name="MyExam_TestQuestionId">场景索引</param>
        public static Task InsertToExam_TestQuestion_Task(Exam_TestQuestionModel MyExam_TestQuestionModel, int MyPaper_TestQuestionId)
        {
            try
            {
                using (var conn = MySqlHelper.GetConnection())
                {
                    var MyPaper_TestQuestion_TaskModels = Paper_TestQuestion_TaskDAL.GetMyPaper_TestQuestion_TaskModel(MyPaper_TestQuestionId);
                    if (MyPaper_TestQuestion_TaskModels != null)
                    {
                        foreach (var MyPaper_TestQuestion_TaskModel in MyPaper_TestQuestion_TaskModels)
                        {
                            Exam_TestQuestion_TaskModel exam_TestQuestion_TaskModel = new Exam_TestQuestion_TaskModel();
                            exam_TestQuestion_TaskModel.ExamtestQuestionId = MyExam_TestQuestionModel.Id;
                            exam_TestQuestion_TaskModel.Describes = MyPaper_TestQuestion_TaskModel.Describes;
                            exam_TestQuestion_TaskModel.MutexTask = MyPaper_TestQuestion_TaskModel.MutexTask;
                            exam_TestQuestion_TaskModel.AfterTask = MyPaper_TestQuestion_TaskModel.AfterTask;
                            exam_TestQuestion_TaskModel.AfterInterval = MyPaper_TestQuestion_TaskModel.AfterInterval;
                            exam_TestQuestion_TaskModel.Order = MyPaper_TestQuestion_TaskModel.Order;
                            exam_TestQuestion_TaskModel.UiOrder = MyPaper_TestQuestion_TaskModel.UiOrder;
                            exam_TestQuestion_TaskModel.TaskType = MyPaper_TestQuestion_TaskModel.TaskType;
                            exam_TestQuestion_TaskModel.TestQuestionId = MyPaper_TestQuestion_TaskModel.TestId;
                            exam_TestQuestion_TaskModel.IsMain = MyPaper_TestQuestion_TaskModel.IsMain;
                            exam_TestQuestion_TaskModel.TaskId = MyPaper_TestQuestion_TaskModel.Id;
                            exam_TestQuestion_TaskModel.ExcuteState = "未执行";
                            var lastId = MySQLExt.InsertIdentityIndex<Exam_TestQuestion_TaskModel>(conn, exam_TestQuestion_TaskModel);
                            exam_TestQuestion_TaskModel.Id = lastId;
                            if (MyExam_TestQuestionModel.Exam_Tasks == null)
                                MyExam_TestQuestionModel.Exam_Tasks = new List<Exam_TestQuestion_TaskModel>();
                            if (!MyExam_TestQuestionModel.Exam_Tasks.Any(model => model.Equals(exam_TestQuestion_TaskModel)))
                                MyExam_TestQuestionModel.Exam_Tasks.Add(exam_TestQuestion_TaskModel);
                        }
                    }
                }
                return Task.CompletedTask;
            }
            catch(Exception ex)
            {
                return null;
            }
        }
    }
}
